#include <stdio.h>

#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>

#include "wave.h"
#include "parameters.h"
#include "kill.h"
#include "exit_status.h"
#include "screen.h"
#include "os.h"
#include "render.h"
#include "control_panel.h"
#include "font.h"
#include "playlist.h"
#include "id3.h"
#include "directory.h"
#include "icon.h"
#include "dpi.h"

extern void main_event_loop();
extern void play_music(const char *path);
extern void retrieve_id3_data(const char *file);

SDL_Window *main_window;
SDL_GLContext main_context;

SDL_Window *control_window;
SDL_GLContext *control_context;

void callback_quit_sdl(void) {
	SDL_Quit();
}

void callback_destroy_main_window(void) {
	SDL_DestroyWindow(main_window);
}

void callback_destroy_main_gl_context(void) {
	SDL_GL_DeleteContext(main_context);
}

void callback_destroy_control_window(void) {
	SDL_DestroyWindow(control_window);
}

void callback_destroy_control_gl_context(void) {
	SDL_GL_DeleteContext(control_context);
}

extern float control_panel_needed;
SDL_HitTestResult control_hittest(SDL_Window* win, const SDL_Point *area, void *data) {
//	if(area->y < (CONTROL_HEIGHT - 40))
//		return SDL_HITTEST_DRAGGABLE;
//	else return SDL_HITTEST_NORMAL;
	if (area->x > dpi_x(C_SIZE) && area->x < control_panel_needed) return SDL_HITTEST_DRAGGABLE;
	return SDL_HITTEST_NORMAL;
}

extern int render_init(char *playlist);

char *initial_working_directory = NULL;
void callback_free_working_directory() {
	free(initial_working_directory);
}

int main(int argc, char **argv) {
	kill_init(256);

	if (argc < 2) {
		puts("You must specify the playlist to play.");
		kill(EX_ERR_NO_PLAYLIST);
	}

	initial_working_directory = dir_get();
	kill_push(callback_free_working_directory);

	dir_set_process();
#ifdef _DEBUG
	/* only necessary for visual studio -- otherwise the executable should be in the root path */
	dir_set("../");
#endif

	dpi_init();

	font_init();
	font_load("font/Raleway-Light.ttf");

	if (SDL_Init(SDL_INIT_EVERYTHING)) {
		kill_sdl("Could not initialize SDL", EX_ERR_SDL_INIT);
	}
	kill_push(callback_quit_sdl);

	screen_retrieve_info();

	if (!SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1))
		SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);

	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

	main_window = SDL_CreateWindow(APP_NAME, 0, 0, screen_width(), dpi_yi(WINDOW_HEIGHT),
		SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS);

	if (!main_window) {
		kill_sdl("Could not open main SDL window", EX_ERR_SDL_CREATE_WINDOW);
	}
	kill_push(callback_destroy_main_window);

	control_window = SDL_CreateWindow(APP_NAME " control panel", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, dpi_xi(CONTROL_WIDTH), dpi_yi(CONTROL_HEIGHT), SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS);
	if (!control_window) {
		kill_sdl("Could not open control SDL window", EX_ERR_SDL_CREATE_WINDOW);
	}
	kill_push(callback_destroy_control_window);

	os_config();

	main_context = SDL_GL_CreateContext(main_window);
	if (!main_context) {
		kill_sdl("Could not initialize OpenGL context", EX_ERR_SDL_CREATE_GL_CONTEXT);
	}
	kill_push(callback_destroy_main_gl_context);

	control_context = SDL_GL_CreateContext(control_window);
	if (!control_context) {
		kill_sdl("Could not initialize control window OpenGL context", EX_ERR_SDL_CREATE_GL_CONTEXT);
	}
	kill_push(callback_destroy_control_gl_context);

	icon_set(control_window, "img/logo.png");

	SDL_ShowWindow(main_window);
	SDL_ShowWindow(control_window);

	SDL_SetWindowHitTest(control_window, control_hittest, NULL);

	SDL_GL_MakeCurrent(control_window, control_context);
	control_init();
	int needed_width = render_init(argv[1]);
	SDL_SetWindowSize(control_window, needed_width, dpi_yi(C_SIZE));

	SDL_GL_MakeCurrent(main_window, main_context);
	render_initialize(screen_width(), WINDOW_HEIGHT);

	int wave_count = screen_width();
	if (wave_count > 1500) wave_count = 1500;
	wave_init(wave_count);

	dir_set(initial_working_directory);

	/* set working directory to playlist location */
	dir_set_file(argv[1]);

	id3_init();

	playlist_init();

	char *file = dir_isolate_file(argv[1]);
	playlist_load(file);
	free(file);
	
	main_event_loop();

	kill(EX_NORMAL);
	return 0;
}